﻿
using System;
using System.Linq;
using System.Data;

using Northwind.Data;

namespace Subsonic2SqlserverDemo
{
    static class CategoriesTester
    {
        // -------------------------------------------------------- //
        #region [ helper methods ]

        public static void Print(this Category category)
        {
            Console.WriteLine("Category<{0},{1}>", category.CategoryID.ToString(), category.CategoryName);
        }

        #endregion

        // -------------------------------------------------------- //
        #region [ test ]

        private static void TestFetchAll()
        {
            using (IDataReader reader = Category.FetchAll())
            {

                int index = 0;
                while (reader.Read())
                {
                    ++index;
                    Console.WriteLine("[{0}] id={1} name={2}",
                        index.ToString(),
                        reader[Category.Columns.CategoryID].ToString(),
                        reader[Category.Columns.CategoryName].ToString());
                }
            }
        }

        private static void TestUserdefSave()
        {
            Category newCategory = new Category
            {
                CategoryName = "cheka test",
                Description = "fake category for test purpose"
            };

            for (int index = 0; index < 5; ++index)
            {
                Product newProd = new Product
                {
                    ProductName = string.Format("TestProduct<{0}>ForNewCategory", index.ToString()),
                    Discontinued = true
                };
                newCategory.AddProduct(newProd);
            }

            // save, including related products
            newCategory.Save();

            Console.WriteLine("DONE !!");
        }

        #endregion

        // -------------------------------------------------------- //
        #region [ main entry ]

        public static void TestMain()
        {
            //TestFetchAll();
            TestUserdefSave();
        }

        #endregion
    }
}